import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Entry {
	private String path;
	
	//constructor
	public Entry(){}
	public Entry(String p){
		path = p;
	}

	
	
	public void start(){
		List<Departure> dList = new ArrayList<Departure>();
		Fingerprint bf = null;
		int c = 0;
		
		
		//read file
		try{
			String[] files = new File(path).list();		
			for(int i=0; i<files.length; i++){
				BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(path+files[i]))));
				System.out.println("************"+files[i]+"************");

				//get data from file
				String bufferedString = getStringFromReader(MAIN.DWELL, br);
				while(bufferedString != null){
					Fingerprint f = new Fingerprint(MAIN.DWELL, MAIN.HZ, bufferedString.split("\n"));
					departureHandle(f, dList);
					if(c==0){
						bf = new Fingerprint(f);
						if((bufferedString=getStringFromReader(MAIN.DWELL, br)) == null)
							break;
						f = new Fingerprint(MAIN.DWELL, MAIN.HZ, bufferedString.split("\n"));
						departureHandle(f, dList);
					}
					
					//System.out.print(c + " " + bf.tanimotoSimilarity(f));
					if(bf.tanimotoSimilarity(f) >= MAIN.SIM_THRESHOLD){
						bf.combine(f);
						
						if(c < MAIN.CMAX) 
							c++;
						
						if(c==MAIN.CMAX && !f.isValid()){
							bf.setValid(true);           				//set fingerprint valid
							if(!bf.exist(dList)){
								dList.add(new Departure(bf));					//create a departure detection class, add to list
								//System.out.print(" E");
							}

							c=0;  //reset the counter
						}
					}
					else
						c=0;
					//System.out.println(" " + c);
					
					
					
					bufferedString = getStringFromReader(MAIN.DWELL, br);
				}
			}
			System.out.println("Learn Finished!!");
			return;
		}catch(IOException e){
			System.out.println("Entry.start() Error: " + e.getMessage());
			return;
		}
	}



	private void departureHandle(Fingerprint f, List<Departure> dList) {
		//clear items which t=0
		for(Iterator<Departure> i = dList.listIterator(); i.hasNext();)
			if(i.next().getT() == 0)
				i.remove();
		
		for(int i=0; i<dList.size(); i++)
			dList.get(i).depDetection(f);
	}



	private String getStringFromReader(int DWELL, BufferedReader br) {
		String str="";
		for(int i=0; i<DWELL; i++){
			try {
				String line = br.readLine();
				while(true){
					if(line == null){
						return null;
					}
					else{
						str += line+"\n";
						if(line.equals("###"))
							break;
					}	
					
					line = br.readLine();
				}
			} catch (IOException e) {
				System.out.println("### ERROR! getStringFromReader(int DWELL, BufferedReader br): " + e.getMessage());
				i--;
				continue;
			}
		}
		return str;
	}
}
